/*
 * @Description: 快速导航 数据存储
 * @Author: Rfan
 * @Date: 2022-01-14 13:40:13
 * @LastEditTime: 2022-01-29 18:10:55
 */

import { observable, action } from 'mobx';
import { isNullObj, jsonClone } from '@/utils';

// 观察者
const tagsViewState = observable({
  visitedViews: [],
  current: {},
  homePath: '/welcome'
});

// 方法
const actions = {
  addView: action((view) => {
    if (!view || isNullObj(view) || view?.hidden) return;
    if (tagsViewState.visitedViews.some((v) => v?.path === view?.path)) return;
    tagsViewState.visitedViews.push({ ...view, title: view?.meta?.title || 'no-name' });
  }),

  delView: action((idx) => {
    const tabList = jsonClone(tagsViewState.visitedViews);
    if (!tabList[idx]?.meta?.fixed) {
      tabList.splice(idx, 1);
      tagsViewState.visitedViews = tabList;
    }
  }),

  setTagsView: action((newList) => {
    const fixedList = tagsViewState.visitedViews.filter((item) => item.meta?.fixed);
    tagsViewState.visitedViews = [...fixedList, ...newList];
  })
};

export { tagsViewState, actions };
